package textfield;
import java.util.Hashtable;

import java.util.Stack;
import java.io.*;

import org.xml.sax.*;

import javax.xml.parsers.SAXParserFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser; 
import org.xml.sax.helpers.DefaultHandler;
import java.util.Vector;



/**
 * Describe class Proto here.
 *
 *
 * Created: Sat Jul 24 10:10:20 2010
 *
 * @author <a href="mailto:fred@theseibels.com">Fred Seibel</a>
 * @version 1.0
 */
public final class Proto extends DefaultHandler {
    static Hashtable operations = new Hashtable ();
    static Hashtable patientAnswers = new Hashtable();
    static Vector drugs = new Vector();
    static Hashtable recordAnswers = new Hashtable();
    static Stack completedQuestions = new Stack ();
    static Reconstruct reconstruct;
    static int lang = 2;

    public Proto(InputStream stream, TextFieldDemo tfd) throws Exception {
	SAXParserFactory factory = SAXParserFactory.newInstance();
        try {
            // Parse the input
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse( stream, this );

        } catch (Throwable t) {
            t.printStackTrace ();
        }
	reconstruct = new textfield.Reconstruct();
    }

    Question question = null;
    public void startElement(String uri, String localName, String qName, 
                        Attributes attributes) throws SAXException {
	if (qName.equals("q")) {
	    question = new Question(attributes);
	}
	if (qName.equals("a")) {
	    question.addAnswer(attributes.getValue("n"), 
			       attributes.getValue("t"), 
			       attributes.getValue("d"),
			       attributes.getValue("c"),
			       attributes.getValue("x"));
	}
	if (qName.equals("drug")) {
	    drugs.addElement(new Drug(attributes));
	}
	if (qName.equals("x")) {
	    Action action = new Action(attributes.getValue("o"), attributes.getValue("t") , attributes.getValue("d"));
	}
    }
    
    /* This section is for keeping track of the location in the hierarchy of operations */
    Operation startOp;
    static  Operation nextOp;
    boolean askCountry = true;
 
    void reset() {
	patientAnswers = new Hashtable();
    }
    void initOps() {
	if (askCountry) {
	    startOp = (Operation) operations.get("country");
	    askCountry = false;
	}
	else {
	    startOp =  (Operation) operations.get("top");
	}
	nextOp = startOp;
	completedQuestions.removeAllElements();
    }

    void executeNext(Device device) {
	if (nextOp == null) {
	    addAnswer();
	    completedQuestions = new Stack();
	    initOps();
	}
	if (nextOp instanceof Question) {
	    Question question = (Question) nextOp;
	    addAnswer();
	    completedQuestions.push(question);
	}
	nextOp = nextOp.doOperation(device);
    }
    
    void addAnswer() {
	if (completedQuestions.size() > 0) {
	    Question lastQuestion = (Question) completedQuestions.peek();
	    if (!((String) recordAnswers.get("x")).equals("")) {
		lastQuestion.answer = (String) recordAnswers.get("x");
	    }
	    else {
		lastQuestion.answer = (String) recordAnswers.get("d");
	    }
	}
    }

    void backup(Device device) {
	if (completedQuestions.size() > 1) {
	    nextOp = (Operation) completedQuestions.pop();
	    nextOp = (Operation) completedQuestions.pop();
	}
	else {
	   nextOp = startOp;
	}
	executeNext(device);
    }
}
